package com.leonzhangxf.ioc.standalone;

import com.leonzhangxf.ioc.bean.HelloWorldService;
import com.leonzhangxf.ioc.bean.HierarchyService;
import com.leonzhangxf.ioc.bean.impl.NewHierarchyServiceImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * The demo of use {@link org.springframework.context.annotation.AnnotationConfigApplicationContext}
 * to perform spring IOC.
 *
 * @author leonzhangxf
 */
@Configuration
@ComponentScan({"com.leonzhangxf.ioc.bean", "com.leonzhangxf.ioc.standalone"})
public class ApplicationContextForPrimaryUseStarter {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
            new AnnotationConfigApplicationContext(ApplicationContextForPrimaryUseStarter.class);

        HelloWorldService helloWorldService = applicationContext.getBean(HelloWorldService.class);
        try {
            helloWorldService.helloWorld(System.out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("=============");
        HierarchyService hierarchyService = applicationContext.getBean(NewHierarchyServiceImpl.class);
        hierarchyService.printHierarchyInfo();
    }
}
